/**
 * Created by djh on 2016/11/8.
 * 购物车页js
 *
 */
define(function(require,exports){
    var common = require("common");


    /*-----------start 一些公共函数-------------------*/
    var $checkAll = $(".order_car .check_all");
    var $checkItem = $(".order_car .table1 tbody [type='checkbox']");

    var count_all = function() {
        //计算商品总量,与价格总量
        var all_count = 0;
        var all_num = 0 ;
        var $all_count = $(".all_count");
        var $all_num = $(".all_num");
        var $checked = $(".order_car .table1 tbody [type='checkbox']").filter(":checked");
        if($checked.length>0) {
            $checked.each(function () {
                var count = $(this).parents("tr").find(".count").html();
                var num = $(this).parents("tr").find(".num").val();
                all_count = all_count + Number(count);
                all_num = all_num + Number(num);
            });
        }
        $all_count.html(all_count.toFixed(2));
        $all_num.html(all_num.toFixed(2));
    };count_all();

    //设置相关值
    var set_val = function($tr,num) {
        $tr.find(".num").val(num);
        var price = $tr.find(".price").html();
        var count = Number(price)*Number(num);
        $tr.find(".count").html(count.toFixed(2));
        count_all();
    };
    /*-----------end 一些公共函数-------------------*/
    //数量计算框
    exports.numBox = function(){

        //加减操作
        $(".num_box").on("click",".cut",function(){
            var tr = $(this).parents("tr");
            var $num = tr.find(".num");
            var num = $num.val();
            if(num==1) return false;
            num--;
            var min = $num.data("min");
            if(num<min) {
                layer.alert("该商品的最小交易量为【"+min+"】吨");
                return false;
            }
            set_val(tr,num);
        });
        $(".num_box").on("click",".add",function(){
            var tr = $(this).parents("tr");
            var $num = tr.find(".num");
            var num = $num.val();
            num++;
            var max = $num.data("max");
            if(num>max) {
                layer.alert("该商品的库存仅为【"+max+"】吨");
                return false;
            }
            set_val(tr,num);
        });

        $(".num_box").on("keyup",".num",function(){
            var tr = $(this).parents("tr");
            var $num = tr.find(".num");
            var num = $num.val();
            var min = parseFloat($num.data("min"));
            var max = parseFloat($num.data("max"));

            //var num2 = clearNoNum($num);

            //var reg = /^[^1-9]|[^\d\.]+/g;
            var reg1 = /[^\d.]/g; //清除数字和“.” 以外的字符
            var reg2 = /(\.\d*)\./g; //清除两个以上的"."
            var reg3 = /^(\d+)\.(\d{0,3}).*$/; //清除三位以上的小数

            var num2 = num.replace(reg1,"").replace(reg2,"$1").replace(reg3,"$1.$2");
            if(num2==""){
                num2=min;
            }
            if(num2<min) {
                layer.alert("该商品的最小交易量为【"+min+"】吨");
                num2=min;
            }else if(num2>max) {
                layer.alert("该商品的库存仅为【"+max+"】吨");
                num2=max;
            }
            set_val(tr,num2);
        });

    };
    exports.init = function(){

        if(!!exports.wrong){
            common.error(exports.wrong)
        }
        common.leftNav(exports.leftNavIndex);
        //全选
        var $canCheck = $checkItem.not(":disabled");
        common.checkBox($checkAll,$canCheck,$("#has_checkId"),function(){
            /*if($("#has_checkId").val()!="") {
             $canCheck.each(function(i,e){
             $(this).parents("tr").find("input").not("[type='checkbox']").prop("disabled",false);
             });
             }else {
             $canCheck.each(function(i,e){
             $(this).parents("tr").find("input").not("[type='checkbox']").prop("disabled",true);
             });
             }*/
            $checkItem.each(function(i,e){
                if($(e).is(":checked")) {
                    $(e).parents("tr").find("input").not("[type='checkbox']").prop("disabled",false);
                }else {
                    $(e).parents("tr").find("input").not("[type='checkbox']").prop("disabled",true);
                }
            });
            count_all();
        });
        //初始化 数量操作框
        exports.numBox();
        //删除操作
        $(".table").on("click",'[data-act="del"]',function(){
            var tr = $(this).parents("tr");
            var id = $(this).data("id");
            layer.confirm("确定删除此商品吗？",{icon:3},function(layerIndex){
                var car_num = $(".carNum_id").html();
                if (car_num == "") {
                    car_num = 0;
                }
                common.ajaxHandle({
                    url: exports.delCartProductUrl,
                    data:{id:id},
                    callback:function(){
                        car_num--;
                        $(".carNum_id").addClass("badge").html(car_num);
                        tr.find("[type='checkbox']").prop("checked",false);
                        tr.find("[type='checkbox']").trigger("change");
                        tr.remove();
                        layer.msg('删除成功');
                    }
                });
            });
        });
    };


    /*购物车 提交订单*/
    $(".order_car").on('click','.pay_btn',function () {
        var $form = $(this).parents("form");
        var size = $form.find("[type='checkbox']:checked").size();
        if(size<=0) {
            layer.alert("请选择至少一件商品！！");
            return false;
        }else {
            var attr=[],firstJson={},errorNum=0;
            var $checked = $form.find("tbody [type='checkbox']:checked");
            var firstObj =  $checked.first();
            firstJson.delivery_way = firstObj.siblings("[name='delivery_way[]']").val();
            firstJson.repertory_id = firstObj.siblings("[name='repertory_id[]']").val();

            $checked.each(function(i,e){
                var $this = $(this);
                var json = {};
                json.delivery_way = $this.siblings("[name='delivery_way[]']").val();
                json.repertory_id = $this.siblings("[name='repertory_id[]']").val();

                if(firstJson.delivery_way!=json.delivery_way) {
                    common.error("所选产品不是同一种交收方式,不允许提交订单");
                    errorNum++;
                    return false;
                }else if(firstJson.repertory_id!=json.repertory_id) {
                    common.error("所选产品不在同一个交收仓,不允许提交订单");
                    errorNum++;
                    return false;
                }
            });
            if(errorNum===0) {
                $form.submit();
            }
        }
    });

});
